<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @showDialog="showDialog" />
        <!-- 树形结构 -->
        <el-tree :data="departs" :props="defaultProps">
          <!-- <tree-tools slot-scope="{ data }" ></tree-tools> -->
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepart="getDepartments" @showDialog="showDialog" />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addDept" :is-show-dialog.sync="isShowDialog" :current-node="node" @addDepart="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/department'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Department',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      isShowDialog: false,
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company.name = result.companyName
      this.departs = tranListToTreeData(result.depts, '')
      console.log(this.departs)
    },
    // 编辑和新增(知识点2：默认参数的使用，这里可以不写，但是写了更完美)
    showDialog(treeNode, isEdit = false) {
      // 新增和编辑都需要打开弹层
      this.node = treeNode
      // 1.打开弹层
      this.isShowDialog = true
      // 判断是否回填数据(编辑状态需要回填数据)
      // 调用获取编辑时回填数据的方法，需要让add-dept.vue文件获取数据
      // 2.让弹层调用接口获取回填数据(知识点3:如何控制子组件调用自身的方法)
      // 知识点4：短路运算符的使用
      isEdit && this.$refs.addDept.getEmployeesInfo(treeNode.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
